package com.AAL.Action;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;

import com.AAL.Model.Order;
import com.AAL.DAO.OrderDAO;
import com.AAL.DAO.PaymentOfDealerDAO;
import com.AAL.DAO.OrderDetailDAO;
import com.AAL.Model.Account;
import com.AAL.Model.PaymentOfDealer;
import com.AAL.Model.OrderDetail;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class OrderAction extends ActionSupport implements SessionAware {

	/*
	 * Khai bao cac thuoc tinh
	 */
	private Map<String, Object> session;
	private Account myAccount;

	private List<Order> lstOrder;
	private OrderDAO lnkOrder;
	private Order order;
	private OrderDetailDAO lnkOrderDetail;
	private List<OrderDetail> lstOrderdetail;

	public OrderAction() {

	}

	/*
	 * ---------------------------------------------------------- Minh
	 * ----------------------------------------------------------
	 */

	// lay cac don dat hang cua moi dealer voi cong ty
	public String orderHistory() {
		try {

		} catch (Exception e) {
			return ERROR;
		}
		return SUCCESS;
	}

	/*
	 * ---------------------------------------------------------- Ket thuc Minh
	 * ----------------------------------------------------------
	 */

	/*
	 * ---------------------------------------------------------- Huyen
	 * ----------------------------------------------------------
	 */
	//thu 3 ngay 29/03/2011
	//Huyen
	//Lay tat ca cac don dat hang cua employee hien tai
	public String GetAllOrderOfCurrentEmpaloyee() {	
		try {
			Account myAccount = null;
			if (session.get("myAccount") != null)
				myAccount = (Account) session.get("myAccount");
			
			if (myAccount != null) 
			{
				//Lay khu vuc hien tai
				short regionID= myAccount.getEmployee().getAccount().getAccountInformation().getCity().getRegion().getRegionID();
				System.out.println("regionID:"+regionID);
				lnkOrder =new OrderDAO();	
				//lay tat ca cac don dat hang cua khu vuc hien tai
				this.lstOrder = lnkOrder.GetAllOrderByRegion(regionID);	
			}
		} catch (Exception e) {
			// TODO: handle exception
			return ERROR;
		}		
		return SUCCESS;
	}

	//thu 5 ngay 31/3/2011
	//Huyen
	//Chi tiet don dat hang
	//Lay don dat hang va chi tiet cac mat hang cua don dat hang do	
	public String GetOrderAndOrderDetail() {
		System.out.println("view chi tiet");
		try {
			HttpServletRequest request = (HttpServletRequest) ActionContext
			.getContext().get(ServletActionContext.HTTP_REQUEST);
			int Id=(Integer.parseInt(request.getParameter("id"))); //lay ID cua don dat hang
			lnkOrder=new OrderDAO();
			order=lnkOrder.getOrder(Id);  //lay don dat hang hien tai	
			//lay cac mat hang cua don dat hang
			lnkOrderDetail=new OrderDetailDAO();
			lstOrderdetail=lnkOrderDetail.getLstOrderDetailByOrder(order) ;
			System.out.println("Lay duoc tat ca cac mat hang");
		} catch (Exception e) {			
			addActionError("Error:"+ e.getMessage());
			return ERROR;
		}
		return SUCCESS;
	}

	/*
	 * ---------------------------------------------------------- Ket thuc Huyen
	 * ---------------------------------------------------------
	 */

	@Override
	public void setSession(Map<String, Object> arg0) {
		this.session = arg0;
	}

	public Map<String, Object> getSession() {
		return session;
	}

	public List<Order> getLstOrder() {
		return lstOrder;
	}

	public void setLstOrder(List<Order> lstOrder) {
		this.lstOrder = lstOrder;
	}

	public Order getOrder() {
		return order;
	}

	public void setOrder(Order order) {
		this.order = order;
	}

	public Account getMyAccount() {
		return myAccount;
	}

	public void setMyAccount(Account myAccount) {
		this.myAccount = myAccount;
	}

	public List<OrderDetail> getLstOrderdetail() {
		return lstOrderdetail;
	}

	public void setLstOrderdetail(List<OrderDetail> lstOrderdetail) {
		this.lstOrderdetail = lstOrderdetail;
	}

}
